<?php

namespace app\apiadmin\model\deposit;

use app\common\utils\Res;
use think\facade\Db;
use think\Model;

use app\apiadmin\service\statics\LiveUserGetDataService;

class DepositModel extends Model
{

    protected $name = "siyu_deposit";

    protected $pk = "id";

    public function searchPageList($where,$page,$page_size){
        $state_map=array_column($this->stateList(),'state_name','state');
        $pay_method_map=array_column($this->payMethodList(),'pay_method_name','pay_method');
        $field="d.*,c.name as course_name,c.fee as course_fee,m.nick_name";
        return self::alias('d')
            ->leftJoin('xh_siyu_course c','c.id=d.siyu_course_id')
            // ->leftJoin('xh_siyu_user u', ' u.openid=d.wx_openid')
            ->leftJoin('xh_siyu_sys_member m ', 'm.uid = d.tutor_id')
            ->where($where)
            ->field($field)
            ->order(['id'=>'desc'])
            ->page($page,$page_size)
            ->select()
            ->each(function($item) use($state_map,$pay_method_map){
                $item['state_name']=isset($state_map[$item['state']])?$state_map[$item['state']]:'';
                $item['pay_method_name']=isset($pay_method_map[$item['pay_method']])?$pay_method_map[$item['pay_method']]:'';
                $item['is_pay_order']=!empty($item['siyu_order_id'])?'是':'否';
                $item['fee_name']=bcdiv($item['fee'],100,2);
                $item['actual_payment_fee_name']=bcdiv($item['actual_payment_fee'],100,2);
                $item['course_fee_name']=bcdiv($item['course_fee'],100,2);
                $item['order_fee']=bcsub($item['course_fee'],$item['actual_payment_fee']);
                $item['order_fee_name']=bcdiv($item['order_fee'],100,2);
                $item['is_show_refund']=false;
                if($item['state']==1){
                    $item['is_show_refund']=true;
                }
                $item['pay_url']='';
                if($item['state']==0){
                    $item['pay_url']=env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $item['siyu_course_id'] . '&tutorId=' . $item['tutor_id'] . '&deposit_id='.$item['id'];
                }
                $item['pay_order_url']='';
                if($item['state']==1){
                    $item['pay_order_url']=env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $item['siyu_course_id'] . '&tutorId=' . $item['tutor_id'];
                } 
                $item['nickname']='';
                $item['wx_avatarurl']='';
                $user_info = Db::name('siyu_user')->where(['openid'=>$item['wx_openid']])->find();
                if($user_info){
                    $item['nickname']=$user_info['nickname'];
                    $item['wx_avatarurl']=$user_info['wx_avatarurl'];
                }
            });
    }
    public function searchList($where){
        $state_map=array_column($this->stateList(),'state_name','state');
        $pay_method_map=array_column($this->payMethodList(),'pay_method_name','pay_method');
        $field="d.*,c.name as course_name,c.img";
        return self::alias('d')
            ->leftJoin('xh_siyu_course c','c.id=d.siyu_course_id')
            // ->leftJoin('xh_siyu_user u', ' u.openid=d.wx_openid')
            ->where($where)
            ->group('d.id')
            ->field($field)
            ->order(['id'=>'desc'])
            ->select()
            ->each(function($item) use($state_map,$pay_method_map){
                $item['state_name']=isset($state_map[$item['state']])?$state_map[$item['state']]:'';
                $item['pay_method_name']=isset($pay_method_map[$item['pay_method']])?$pay_method_map[$item['pay_method']]:'';
                $item['fee_name']=bcdiv($item['fee'],100,2);
                $item['actual_payment_fee_name']=bcdiv($item['actual_payment_fee'],100,2);
                $item['nickname']='';
                $item['wx_avatarurl']='';
                $user_info = Db::name('siyu_user')->where(['openid'=>$item['wx_openid']])->find();
                if($user_info){
                    $item['nickname']=$user_info['nickname'];
                    $item['wx_avatarurl']=$user_info['wx_avatarurl'];
                }
            });
    }

    public function searchCount($where){
        return self::alias('d')
            ->leftJoin('xh_siyu_course c','c.id=d.siyu_course_id')
            // ->leftJoin('xh_siyu_user u', ' u.openid=d.wx_openid')
            ->leftJoin('xh_siyu_sys_member m ', 'm.uid = d.tutor_id')
            ->where($where)
            ->group('d.id')
            ->count();
    }
    public function stateList(){
        return [
            ['state'=>0,'state_name'=>'未支付'],
            ['state'=>1,'state_name'=>'已支付'],
            ['state'=>2,'state_name'=>'已退款']
        ];
    }

    public function payMethodList(){
        return [
            ['pay_method'=>1,'pay_method_name'=>'微信'],
            ['pay_method'=>2,'pay_method_name'=>'支付宝'],
            ['pay_method'=>3,'pay_method_name'=>'银行卡']
        ];
    }

    public function add($data){
        return self::create($data);
    }

    public function edit($where,$data){
        return self::where($where)->update($data);
    }
    public function info($where){
        $field="*";
        return self::where($where)->field($field)->find();
    }
}
